package web.fabrimind.tas.service.impl;

import java.util.List;
import web.fabrimind.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import web.fabrimind.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import web.fabrimind.tas.domain.MaterialInboundApplyDetail;
import web.fabrimind.tas.mapper.MaterialInboundApplyMapper;
import web.fabrimind.tas.domain.MaterialInboundApply;
import web.fabrimind.tas.service.IMaterialInboundApplyService;

/**
 * 物料入库申请Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-03
 */
@Service
public class MaterialInboundApplyServiceImpl implements IMaterialInboundApplyService
{
    @Autowired
    private MaterialInboundApplyMapper materialInboundApplyMapper;

    /**
     * 查询物料入库申请
     *
     * @param applyId 物料入库申请主键
     * @return 物料入库申请
     */
    @Override
    public MaterialInboundApply selectMaterialInboundApplyByApplyId(Long applyId)
    {
        return materialInboundApplyMapper.selectMaterialInboundApplyByApplyId(applyId);
    }

    /**
     * 查询物料入库申请列表
     *
     * @param materialInboundApply 物料入库申请
     * @return 物料入库申请
     */
    @Override
    public List<MaterialInboundApply> selectMaterialInboundApplyList(MaterialInboundApply materialInboundApply)
    {
        return materialInboundApplyMapper.selectMaterialInboundApplyList(materialInboundApply);
    }

    /**
     * 新增物料入库申请
     *
     * @param materialInboundApply 物料入库申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMaterialInboundApply(MaterialInboundApply materialInboundApply)
    {
        materialInboundApply.setCreateTime(DateUtils.getNowDate());
        int rows = materialInboundApplyMapper.insertMaterialInboundApply(materialInboundApply);
        insertMaterialInboundApplyDetail(materialInboundApply);
        return rows;
    }

    /**
     * 修改物料入库申请
     *
     * @param materialInboundApply 物料入库申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMaterialInboundApply(MaterialInboundApply materialInboundApply)
    {
        materialInboundApply.setUpdateTime(DateUtils.getNowDate());
        materialInboundApplyMapper.deleteMaterialInboundApplyDetailByApplyId(materialInboundApply.getApplyId());
        insertMaterialInboundApplyDetail(materialInboundApply);
        return materialInboundApplyMapper.updateMaterialInboundApply(materialInboundApply);
    }

    /**
     * 批量删除物料入库申请
     *
     * @param applyIds 需要删除的物料入库申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMaterialInboundApplyByApplyIds(Long[] applyIds)
    {
        materialInboundApplyMapper.deleteMaterialInboundApplyDetailByApplyIds(applyIds);
        return materialInboundApplyMapper.deleteMaterialInboundApplyByApplyIds(applyIds);
    }

    /**
     * 删除物料入库申请信息
     *
     * @param applyId 物料入库申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMaterialInboundApplyByApplyId(Long applyId)
    {
        materialInboundApplyMapper.deleteMaterialInboundApplyDetailByApplyId(applyId);
        return materialInboundApplyMapper.deleteMaterialInboundApplyByApplyId(applyId);
    }


    /**
     * 查询用户(申请人和审核人)列表（不分页）
     */
    @Override
    public List<MaterialInboundApply> selectUserList(MaterialInboundApply materialInboundApply) {
        return materialInboundApplyMapper.selectUserList(materialInboundApply);
    }

    /**
     * 查询供应商列表（不分页）
     */
    @Override
    public List<MaterialInboundApply> selectSupplierList(MaterialInboundApply materialInboundApply) {
        return materialInboundApplyMapper.selectSupplierList(materialInboundApply);
    }


    /**
     * 新增物料入库申请明细信息
     *
     * @param materialInboundApply 物料入库申请对象
     */
    public void insertMaterialInboundApplyDetail(MaterialInboundApply materialInboundApply)
    {
        List<MaterialInboundApplyDetail> materialInboundApplyDetailList = materialInboundApply.getMaterialInboundApplyDetailList();
        Long applyId = materialInboundApply.getApplyId();
        if (StringUtils.isNotNull(materialInboundApplyDetailList))
        {
            List<MaterialInboundApplyDetail> list = new ArrayList<MaterialInboundApplyDetail>();
            for (MaterialInboundApplyDetail materialInboundApplyDetail : materialInboundApplyDetailList)
            {
                materialInboundApplyDetail.setApplyId(applyId);
                list.add(materialInboundApplyDetail);
            }
            if (list.size() > 0)
            {
                materialInboundApplyMapper.batchMaterialInboundApplyDetail(list);
            }
        }
    }
}




